package study.top.mydb;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import study.top.common.DBHandler;
import study.top.common.DateTime;
import study.top.mydb.items.WishListItem;

public class WishList {
	static Logger logger = Logger.getLogger(WishList.class);
	// �����ͺ��̽� ���� ��ü�� ���� ����
	DBHandler db = null;

	public WishList() {
		// DB��ü�� ���� ���� ����
		db = DBHandler.getInstance();
	}

	private static final String TABLE = "wishlist";

	/**
	 * ���ø���Ʈ �߰�
	 * 
	 * @param member_seq
	 *            ȸ�� �Ϸù�ȣ
	 * @return int seq ��� �Ϸù�ȣ
	 */
	public int add(int member_seq) {

		int seq = -1;
		logger.debug("==========method start >> add");
		logger.debug("[PARAMS] member_seq=" + member_seq);
		int wishlistno = db.getMax(TABLE, "wishlist_seq", null) + 1;
		logger.debug("�ڵ� ��� ���ø���Ʈ ��ȣ : " + wishlistno);
		// Ʈ�����ǿ� ���� �Ǵ��� �� ���� �ʴ´ٸ�, ������ �ϸ� �ȴ�.
		db.beginTrans();

		// INSERT ó���� ���� ����
		String sql = "INSERT INTO wishList(member_seq, reg_date,edit_date) VALUES(%d,'%s','%s');";
		String date = DateTime.getInstance().getNowDateTimeString();

		boolean result = db.execSql(sql, member_seq, date, date);

		// -----------------------------------------
		// ���ó��
		// -----------------------------------------
		if (result) {
			seq = member_seq;
			db.commit();
			logger.debug("ó����� >> " + seq + "�� ȸ�� ���ø���Ʈ ��� ����");
		} else {
			db.rollback();
			logger.error("ó����� >> ������ �߰� ����");
		}

		logger.debug("==========method end >> add");
		return seq;
	}

	/**
	 * ���� ����
	 * 
	 * @param seq
	 *            �Ϸù�ȣ
	 * @param budget
	 *            ����
	 * @return boolean result
	 */
	public boolean setBudget(int seq, int budget) {
		boolean result = false;
		logger.debug("==========method start >> setBudget");
		logger.debug("[PARAMS] seq=" + seq);
		logger.debug("[PARAMS] budget=" + budget);

		db.beginTrans();

		String sql = "UPDATE wishlist SET budget=%d WHERE seq=%d ;";
		result = db.execSql(sql, budget, seq);

		if (result) {
			db.commit();
			logger.debug("ó����� >> " + seq + "�� ���ø���Ʈ ���꼳�� ����");
		} else {
			db.rollback();
			logger.error("ó����� >> " + seq + "�� ���ø���Ʈ ���꼳�� ����");
		}

		logger.debug("==========method end >> setBudget");
		return result;
	}

	/**
	 * ���ø���Ʈ ����
	 * 
	 * @param member_seq
	 *            ȸ�� �Ϸù�ȣ
	 * @return ProductItem list
	 */
		public WishListItem show(int member_seq) {
		logger.debug("==========method start >> show");
		logger.debug("[PARAMS] member_seq=" + member_seq);
		WishListItem item = null;

		// Ʈ������
		db.beginTrans();

		String sql = "SELECT seq,member_seq,budget,reg_date,edit_date FROM wishlist WHERE member_seq=%d";
		ResultSet rs = db.getResult(sql, member_seq);

		if (rs != null) {
			// ������ ��ȸ ��� �ִ� ���
			try {
				rs.next();

				// 1) ResultSet�� ��� �����͸� ������ ����
				int sno = rs.getInt(1);
				int member_seq1 = rs.getInt(2);
				int budget = rs.getInt(3);
				String reg_date = rs.getString(4);
				String edit_date = rs.getString(5);

				// 2) ����� ���� ��ü�� ����
				item = new WishListItem(sno, member_seq1, budget, reg_date,
						edit_date);

			} catch (SQLException e) {
				logger.error(e);
			} finally {
				// 3) ������ ��ȸ�� ���� �޸� �ݳ�
				try {
					rs.close();
				} catch (SQLException e) {
				}
				rs = null;
			}
		}

		if (item == null) {
			logger.error("���ø���Ʈ �������� ����");
		} else {
			logger.debug("���ø���Ʈ �������� ����");
			logger.debug(item.toString());
		}

		logger.debug("==========method end >> show");
		return item;
	}

	/**
	 * �����ϱ�
	 * 
	 * @return boolean result �����ϱ�
	 */
	public boolean isPayment() {
		boolean result = false;
		logger.debug("==========method start >> isPayment");

		logger.debug("==========method end >> isPayment");
		return result;
	}
	/**
	 * 
	 * @param member_seq
	 * @return
	 */
	
	/**
	 * ���ø���Ʈ ����
	 * 
	 * @param member_seq
	 *            ȸ�� �Ϸù�ȣ
	 * @return result boolean ��������
	 */
	public boolean delete(int member_seq) {
		boolean result = false;
		logger.debug("==========method start >> delete");
		logger.debug("[PARAMS] member_seq=" + member_seq);

		db.beginTrans();

		String sql = "DELETE FROM wishlist WHERE member_seq = %d";
		result = db.execSql(sql, member_seq);

		if (result) {
			db.commit();
			logger.debug("ó����� >> " + member_seq + "�� ȸ�� ���ø���Ʈ���� ���� ����");
		} else {
			db.rollback();
			logger.error("ó����� >>" + member_seq + "�� ȸ�� ���ø���Ʈ���� ���� ���� ");
		}
		logger.debug("==========method end >> delete");
		return result;
	}
}
